// pages/regist/regist.js
Page({
  data: {
    name: '',
    admin: '',
    pwd: '',
    checkPwd: '',
    userType:'C',
    eyeType: true,
    passwordType: true,
    eyeType2: true,
    passwordType2: true,
  },
  //获取输入的值
  getName(e) {
    // console.log(e.detail.value)
    this.setData({
      name: e.detail.value
    })
  },
  getAdmin(e) {
    // console.log(e.detail.value)
    this.setData({
      admin: e.detail.value
    })
  },
  getpwd(e) {
    // console.log(e.detail.value)
    this.setData({
      pwd: e.detail.value
    })
  },
  getcheckPwd(e) {
    // console.log(e.detail.value)
    this.setData({
      checkPwd: e.detail.value
    })
  },
  //密码是否可见
  eyeStatus() {
    this.setData({
      passwordType: !this.data.passwordType,
      eyeType: !this.data.eyeType,
    })
  },
  eyeStatus2() {
    this.setData({
      passwordType2: !this.data.passwordType2,
      eyeType2: !this.data.eyeType2,
    })
  },
  //提交注册
  commit() {
    wx.showLoading({
      title: '注册中...',
    })
    //得到输入的各项值
    let name = this.data.name
    let admin = this.data.admin
    let pwd = this.data.pwd
    let checkPwd = this.data.checkPwd
    let userType= this.data.userType
    console.log(name, admin, pwd, checkPwd,userType)
    //校验输入的各项值
    if (name.length < 2) {
      wx.showToast({
        icon: 'none',
        title: '用户名不能低于2个字符',
      })
      return
    }
    if (admin.length < 3) {
      wx.showToast({
        icon: 'none',
        title: '账号不能少于3个字符',
      })
      return
    }
    if (pwd.length < 6) {
      wx.showToast({
        icon: 'none',
        title: '密码不能少于6个字符',
      })
      return
    }
    if (pwd != checkPwd) {
      wx.showToast({
        icon: 'none',
        title: '两次密码输入不一致',
      })
      return
    }
    //验证账号是否被用过，保证账号的唯一性
    wx.cloud.database().collection('user').where({
      admin: admin //到云数据库找与本地输入的账号相同的账号的那条信息
    }).get({
      success(res) {
        console.log('数据获取成功', res)
        let user = res.data[0] //到user数据库获取与输入账号对应的个人信息到user变量
        console.log('user', user)
        if (!user) {//如果该账号没有被注册过，开始真正的注册
          wx.showLoading({
            title: '注册中...',
          })
          //把输入的值传到云数据库
          wx.cloud.database().collection('user').add({
            data: {
              name: name,
              admin: admin,
              pwd: pwd,
              userType:userType
            },
            success(res) {
              console.log("注册成功", res)
              wx.hideLoading({
                success: (res) => {
                  wx.showToast({
                    title: '注册成功',
                  })
                },
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/login/login?admin=' + admin,
                })
              }, 1000);
            },
            fail(res) {
              console.log('注册失败', res)
              wx.hideLoading({
                success: (res) => {
                  wx.showToast({
                    icon: 'none',
                    title: '注册失败',
                  })
                },
              })
            }
          })
        } else {
          console.log('账号已经存在')
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                icon: 'none',
                title: '账号已经存在，请重新输入',
              })
            },
          })
         
        }

      },
      fail(res) {
        console.log('数据获取失败', res)
      }
    })

  },

})